/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pt.mleiria.utils;

import java.util.Random;

/**
 *
 * @author manuel
 */
public final class RndGenerator {

    private static long seed;
    private static Random rnd;
    
    static{
        seed = System.currentTimeMillis();
        rnd = new Random(seed);
    }
    /**
     * 
     */
    private RndGenerator() {}
    
    
    /**
     * 
     * @return 
     */
    public static long getSeed(){
        return seed;
    }
    /**
     * 
     * @param s 
     */
    public static void setSeed(long s){
        seed = s;
        rnd = new Random(seed);
    }
    /**
     * 
     * @param low
     * @param high
     * @param size
     * @return 
     */
    public static int[] genArrInts(final int low, final int high, int size){
        
        int[] a = new int[size];
        for(int i = 0; i < size; i++){
            a[i] = rnd.nextInt(high-low) + low;
        }
        return a;
    }
    /**
     * 
     * @return  a uniform real number in [0,1)
     */
    public static double uniform(){
        return rnd.nextDouble();
    }
    /**
     * @return a boolean, which is true with probability p, and false otherwise.
     * @throws IllegalArgumentException unless <tt>p >= 0.0</tt> and <tt>p <= 1.0</tt>
     * @param p 
     */
    public static boolean bernoulli(double p){
        if(p < 0.0 || p > 1.0){
            throw new IllegalArgumentException("Probability p must be between 0.0 and 1.0");
        }
        return uniform() < p;
    }
}
